//
//  CNBBSPostedSecionViewController.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/6/7.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CNBBSPostedSecionViewController.h"
#import "CNBBSScoreCell.h"
#import "CNBBSSectionCell.h"
#import "CNBBSForumListViewModel.h"
#import <Masonry/Masonry.h>
#import <CNBaseTool/CNCategoryHeader.h>

@interface CNBBSPostedSecionViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *leftTableView;

@property (nonatomic, strong) UITableView *rightTableView;

@property (nonatomic, strong) CNBBSForumListViewModel *forumVM;

@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation CNBBSPostedSecionViewController

-(CNBBSForumListViewModel *)forumVM
{
    if (!_forumVM) {
        _forumVM = [[CNBBSForumListViewModel alloc]init];
    }
    return _forumVM;
}

- (UITableView *)leftTableView
{
    if (!_leftTableView) {
        _leftTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
        _leftTableView.showsVerticalScrollIndicator = YES;
        _leftTableView.theme_backgroundColor = pageBGColorPicker;
        _leftTableView.tableFooterView = [UIView new];
        [_leftTableView registerClass:[CNBBSSectionCell class] forCellReuseIdentifier:@"LeftCell"];
        [self.view addSubview:_leftTableView];
        [_leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.bottom.mas_equalTo(0);
            make.right.mas_equalTo(self.rightTableView.mas_left).mas_equalTo(0);
            make.width.mas_equalTo(kWindowW * 0.4);
        }];
        
        //去掉分割线
        _leftTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            _leftTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        };
    }
    return _leftTableView;
}

- (UITableView *)rightTableView
{
    if (!_rightTableView) {
        _rightTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _rightTableView.delegate = self;
        _rightTableView.dataSource = self;
        _rightTableView.showsVerticalScrollIndicator = YES;
        _rightTableView.theme_backgroundColor = pageBGColorPicker;
        _rightTableView.tableFooterView = [UIView new];
        [_rightTableView registerClass:[CNBBSScoreCell class] forCellReuseIdentifier:@"RightCell"];
        [self.view addSubview:_rightTableView];
        [_rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
        //去掉分割线
        _rightTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        if (@available(iOS 11.0, *)) {
            _rightTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        };
        
    }
    return _rightTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.leftTableView.hidden = NO;
    self.rightTableView.hidden = NO;
    self.view.theme_backgroundColor = pageBGColorPicker;
    self.title = @"选择版块";
    [Factory addBlackItemToVC:self];
//    if ([CNThemesManager isNight])
//    {
//        //切换背景为夜间颜色
//        [self wr_setNavBarBarTintColor:slideBarNightTextColor];
//        [self wr_setNavBarTitleColor:navigationBarNightTitleColor];
//        [self wr_setStatusBarStyle:UIStatusBarStyleLightContent];
//    }
//    else
//    {
//        //切换背景为白天颜色
//        [self wr_setNavBarBarTintColor:slideBarTextColor];
//        [self wr_setNavBarTitleColor:navigationBarTitleColor];
//        [self wr_setStatusBarStyle:UIStatusBarStyleDefault];
//    }
    //添加分割线
//    UIView *lineView = [[UIView alloc]init];
//    lineView.theme_backgroundColor = bbsSectionLineColorPicker;
//    [self.view addSubview:lineView];
//    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(0);
//        make.top.mas_equalTo(0);
//        make.height.mas_equalTo(0.5);
//    }];
//    lineView.layer.zPosition = 10;
    
    self.selectIndex = 0;
    
    [self refreshData];
}



#pragma mark - 获取数据
//刷新数据
- (void)refreshData
{
    [self.forumVM getDataFromNetCompleteHandle:^(NSError *error) {
        
        if (!error)
        {
            if (self.forumVM.code == 200)
            {
                //AF的回调 是在主线程中
                [self.leftTableView reloadData];
                [self.rightTableView reloadData];
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
                [self.leftTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
                if ([self.leftTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
                    [self.leftTableView.delegate tableView:self.leftTableView didSelectRowAtIndexPath:indexPath];
                }
            }
            else
            {
                [self showErrorMsg:@"加载失败，请稍后再试"];
            }
            
        }else{
            [self showError:error];;
        }
    }];
}

#pragma mark - TableView DataSource Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_leftTableView == tableView)
    {
        return self.forumVM.rowNumber;
    }
    else
    {
        return [self.forumVM childRowNumberForRow:self.selectIndex];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (_leftTableView == tableView)
    {
        CNBBSSectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeftCell" forIndexPath:indexPath];
        cell.titleLb.text = [self.forumVM nameForRow:indexPath.row];
        cell.lineView.hidden = NO;
        return cell;
    }
    else
    {
        CNBBSScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RightCell" forIndexPath:indexPath];
        cell.titleLb.text = [self.forumVM nameForRow:indexPath.row AndSection:self.selectIndex];
        cell.lineView.hidden = YES;
        return cell;
    }
    
    return nil;
}


#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 44;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (_leftTableView == tableView)
    {
        self.selectIndex = indexPath.row;
        [_rightTableView reloadData];
    }
    else
    {
        
        self.sectionBlock([NSString stringWithFormat:@"%@-%@",[self.forumVM nameForRow:self.selectIndex],[self.forumVM nameForRow:indexPath.row AndSection:self.selectIndex]],[self.forumVM urlnameForRow:indexPath.row AndSection:self.selectIndex],[self.forumVM techForumForRow:indexPath.row AndSection:self.selectIndex]);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
